<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class ClassoneController extends Controller
{
     public function index(Request $request)
    {
    	$whereone = [];
    	$db = DB::table('classone');
    	if ($request->has('classname')) {
    		$classname = $request->input('classname');
    		$db -> where('classname','like',"%{$classname}%");
    		$whereone['classname'] = $classname;
    	}
    	if ($request->has('level')) {
    		$level = $request->input('level');
    		$db -> where('level','=',$level);
    		$whereone['level'] = $level;
    	}
    	$one = $db->paginate(6);
    	$odd = DB::table('classodd')->get();
    	$even = DB::table('classeven')->get();
    	return view('admin.classone.index',['one'=>$one,'odd'=>$odd,'even'=>$even,'whereone'=>$whereone]);
    }
     public function destroy($id)
    {
    	// dd($id);
    	$m = DB::table('classone')->where('oneid',$id)->delete();
    	// dd($m);
    	if ($m>0) {
    		return redirect('/admin/classone');
    	}else{
    		return redirect('/admin/classone');
    	}
    }
    public function edit($id)
    {
        $one = DB::table('classone')->where('oneid',$id)->first();
        return view('admin.classone.edit',['one'=>$one]);
    }
    public function update($id,Request $request)
    {
        $class = DB::table('classone')->where('oneid',$id)->get();
        $classname = $class[0]->classname;
        $goodsname = $request->input('classname');
        // 修改商品表里面名字相同的字段
        $goods = DB::table('goods')->get();
        foreach ($goods as $k => $v) {
            if ($classname == $v->goodsname ) {
                $goodsid = $v->goodsid;
                $goodsda = array('goodsname'=>$goodsname);
                DB::table('goods')->where('goodsid',$goodsid)->update($goodsda);
            }
        }
        // 修改单行分类表里面名字相同的字段
        $odd = DB::table('classodd')->get();
        foreach ($odd as $k => $v) {
            if ($classname == $v->classname ) {
                $oddid = $v->oddid;
                $oddda = array('classname'=>$goodsname);
                DB::table('classodd')->where('oddid',$oddid)->update($oddda);
            }
        }
        // 修改双行分类表里面名字相同的字段
        $even = DB::table('classeven')->get();
        foreach ($even as $k => $v) {
            if ($classname == $v->classname ) {
                $evenid = $v->evenid;
                $evenda = array('classname'=>$goodsname);
                DB::table('classeven')->where('evenid',$evenid)->update($evenda);
            }
        }
        // 修改自己表里面名字相同的字段
        $onedata = DB::table('classone')->get();
        foreach ($onedata as $k => $v) {
            if ($classname == $v->classname ) {
                $oneid = $v->oneid;
                $oneda = array('classname'=>$goodsname);
                DB::table('classone')->where('oneid',$oneid)->update($oneda);
            }
        }
    	$data = $request->only('classname','status');
    	$m = DB::table('classone')->where('oneid',$id)->update($data);
        if($m>0){
    		return redirect('/admin/classone');
    	}else{
    		return redirect('/admin/classone');
    	}
    }
    public function create()
    {
        return view('admin.classone.add');

    }
    public function ajax()
    {
        // echo $_GET['cid'];
        $list = DB::table('classone')->where('cid','=',$_GET['cid'])->get();
        echo $list;
    }
    public function store(Request $request)
    {
    	// dd($request);
    	if($request->has('classvalue') && $request->has('status') ){
	        if ($request->input('classname')===0) {
                $status = $request->input('status');
                // dd($status);
	            $data = array('classname'=>$request->input('classvalue'),'cid'=>0,'level'=>1,'status'=>$status);
	            $m = DB::table('classone')->insertGetId($data);
	               if ($m>0) {
	                    return redirect('/admin/classone');
	                }else{
	                    return back();
	                }
	        }     
	        if ($request->has('cid')===false && $request->input('classname')!=0) {
	        	$classname = $request->input('classvalue');
	        	$cid = $request->input('classname');
	        	$level = 2;
                $status = $request->input('status');
	            $data = array('classname'=>$classname,'cid'=>$cid,'level'=>$level,'status'=>$status);
	            $m = DB::table('classone')->insertGetId($data);
	              if ($m>0) {
	                   return redirect('/admin/classone');
	               }else{
	                   return back();
	               }
	       	}
	       	if($request->input('classname')=='--请添加--'){
	       		$classname = $request->input('classvalue');
	       		$cid = $request->input('cid');
	       		$level = $request->input('level');
                $status = $request->input('status');
	       		$data = array('classname'=>$classname,'cid'=>$cid,'level'=>$level,'status'=>$status);
	            $m = DB::table('classone')->insertGetId($data);
	              if ($m>0) {
	                   return redirect('/admin/classone');
	               }else{
	                   return back();
	               }
	       	}
	       	if($request->input('classname')!='--请添加--'){
	       		$classname = $request->input('classvalue');
	       		$cid = $request->input('classname');
	       		$level = $request->input('level')+1;
                $status = $request->input('status');
	       		$data = array('classname'=>$classname,'cid'=>$cid,'level'=>$level,'status'=>$status);
	            $m = DB::table('classone')->insertGetId($data);
	              if ($m>0) {
	                   return redirect('/admin/classone');
	               }else{
	                   return back();
	               }
	       	}
	    }else{
            return back()->with('msg','信息不能为空');
        }
    }
}
